<!--报警规则-->
<template>
  <div class="page-container main">
    <div class="tool-container">
      <el-form class="tool-search" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item>
          <el-select v-model="queryParams.type" placeholder="报警类型" clearable>
            <el-option v-for="item in dict.station_error_reason" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-form-item>
            <el-date-picker v-model="times" type="datetimerange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss" />
          </el-form-item>
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>

        <el-button type="primary" icon="Plus" @click="handleAdd" style="float:right">新建规则</el-button>
      </el-form>
    </div>

    <div class="tabel-container myTable">
      <vxe-table ref="tableRef" :data="tableData" v-loading="loading">
        <vxe-table-column :resizable="true" type="seq" title="序号" width="80" sortable></vxe-table-column>
        <vxe-table-column :resizable="true" field="ruleName" title="报警名称" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="alertType" title="报警类型" sortable>
          <template #default="{ row }">
            {{ marchType(row.alertType,dict.station_error_reason)}}
          </template>
        </vxe-table-column>
        <vxe-table-column :resizable="true" field="timeRange" title="报警时限(小时)" sortable>
          <template #default="{ row }">
            {{ row.timeRange / 3600.0 }}
          </template>
        </vxe-table-column>
        <vxe-table-column :resizable="true" field="times" title="报警次数" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="createByName" title="创建人" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="createTime" title="创建时间" sortable> </vxe-table-column>
        <vxe-table-column :resizable="true" field="enabled" title="状态" sortable>
          <template #default="{ row }">
            {{ marchType(row.enabled,enabled)}}
          </template>
        </vxe-table-column>
        <vxe-column field="id" title="操作" width="260" fixed="right" show-overflow>
          <template #default="{ row }">
            <el-button link type="primary" @click="handleInfo(row)">详情</el-button>
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDel(row)">删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
    </div>
    <div class="myPager">
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
  </div>
</template>

<script lang="ts">
import { list, del } from '@/api/operation/alarmRule/index';
import common from '@/utils/common.ts';

export default {
  props: {
    data: {}
  },
  computed: {
    times: {
      get() {
        return [this.queryParams.timeFrom, this.queryParams.timeTo];
      },
      set(value) {
        this.queryParams.timeFrom = value[0];
        this.queryParams.timeTo = value[1];
      }
    }
  },
  data() {
    return {
      proxy: null,
      dict: {},
      loading: false,
      tableData: [],
      total: 1,
      queryParams: {
        alertType: '1',
        type: '',
        pageNum: 1,
        pageSize: 20
      },
      enabled: [
        { label: '停用', value: '0' },
        { label: '启用', value: '1' }
      ]
    };
  },
  mounted() {
    const { proxy } = getCurrentInstance() as ComponentInternalInstance;
    this.proxy = proxy;
    this.dict = toRefs<any>(this.proxy?.useDict('station_error_reason'));

    this.init();
  },
  methods: {
    init(data) {
      if (data == null) data = this.data;
      this.getList();
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /**
     * 查询数据
     */
    getList() {
      this.loading = true;
      list(this.queryParams).then((res) => {
        this.loading = false;
        if (res.code == 200) {
          this.tableData = res.rows;
        } else {
          ElMessage.error(res.msg);
        }
      });
    },
    handleAdd() {
      let data = {
        ruleName: '',
        alertType: '1',
        type: '',
        timeRange: 0,
        times: 0,
        enabled: '1'
      };
      this.$emit('showRight', 'alarmEdit', '新建规则', data);
    },
    handleInfo(row) {
      this.$emit('showRight', 'alarmInfo', '规则详情', row);
    },
    handleEdit(row) {
      this.$emit('showRight', 'alarmEdit', '编辑规则', row);
    },
    handleDel(row) {
      ElMessageBox.confirm('是否确认要删除该数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true;
          let ids = [row.ruleId];
          del(ids)
            .then((res) => {
              this.loading = false;
              if (res.code == 200) {
                this.getList();
              } else {
                ElMessage.error(res.msg);
              }
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {});
    },
    /**
     * 匹配结果
     */
    marchType(value, types) {
      for (let i = 0; i < types.length; i++) {
        let type = types[i];
        if (type.value == value) return type.label;
      }
    }
  }
};
</script>
